<template>
  <div>
    <el-card>
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item label="审批人">
          <el-input placeholder="审批人"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary">查询</el-button>
          <el-button type="primary" @click="handleAdd">添加</el-button>
        </el-form-item>
      </el-form></el-card
    >

    <el-table :data="tableData" style="width: 100%" border>
      <el-table-column label="Id" width="180">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户名" width="180">
        <template slot-scope="scope">
          <el-popover trigger="hover" placement="top">
            <p>姓名: {{ scope.row.username }}</p>
            <p>住址: {{ scope.row.address }}</p>
            <div slot="reference" class="name-wrapper">
              <el-tag size="medium">{{ scope.row.username }}</el-tag>
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="备注" prop="remarks"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 静态的一个膜态框，用于增加和修改 -->

    <el-dialog title="用户信息" :visible.sync="dialogFormVisible">
      <el-form :model="formData">
        <el-form-item label="用户名" :label-width="formLabelWidth">
          <el-input v-model="formData.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="备注" :label-width="formLabelWidth">
          <el-input v-model="formData.remarks" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleCancel">取 消</el-button>
        <el-button type="primary" @click="handleSave">确 定</el-button>
      </div>
    </el-dialog>

    <div class="block">
      <span class="demonstration">完整功能</span>
      <el-pagination
        @size-change="handlePageSizeChange"
        @current-change="handlePageIndexChange"
        :current-page="pager.pageIndex"
        :page-size="pager.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pager.rowsTotal"
        background
      >
      </el-pagination>
    </div>
  </div>
</template>


<script>
import { getList, addUser, modUser } from "../api/users";

export default {
  data() {
    return {
      tableData: [],
      pager: {
        pageIndex: 1,
        pageSize: 10,
        rowsTotal: 30,
      },
      dialogFormVisible: false,
      formData: {
        id: 0,
        username: "",
        password: "113",
        remarks: "",
      },
      formLabelWidth: "120px",
      currentIndex: 0,
    };
  },

  mounted() {
    this.fetcData(this.pager);
    
  },

  methods: {
    // 用户添加
    handleAdd() {
      this.dialogFormVisible = true;
      this.formData.id = 0;
      this.formData.username = "";
      this.formData.remarks = "";
    },
    //  用户修改
    handleEdit(index, row) {
      this.dialogFormVisible = true;
      this.formData.id = row.id;
      this.formData.username = row.username;
      this.formData.remarks = row.remarks;

      // 编辑的时候可以获取得到数组中的下标
      this.currentIndex = index;

      console.log(index, row);
    },
       // 删除用户
    handleDelete(index, row) {
      console.log(index, row);
    },
    // (无论用户添加还是修改后，保存用户信息)
    handleSave() {
      this.dialogFormVisible = false;
      console.log(this.formData);
      // 如果一个表单中的id为0,则认为是添加用户，反之就是修改用户
      if (this.formData.id === 0) {
        addUser(this.formData)
          .then(({ data }) => {
            console.log(data);
            // 将新添加的数据push到现有的数据列表的最后一行里面
            this.tableData.push(data.data);
          })
          .catch((err) => {
            console.log(err);
          });
      } else {
        modUser(this.formData.id, this.formData)
          .then(({ data }) => {
            console.log(data);

            // 修改的时候可以获取表数据的下标
            this.tableData.splice(this.currentIndex, 1, data.data);
          })
          .catch((err) => {
            console.log(err);
          });
      }
    },
    // 取消
    handleCancel() {
      this.dialogFormVisible = false;
    },
     // 页面页码发生改变的时候，重新拉取数据
    handlePageIndexChange(val) {
      console.log(val);
      this.pager.pageIndex = val>0?val:1;
      this.fetcData(this.pager);
    },
    // 页面改变的时候，重新拉取数据
    handlePageSizeChange(val) {
      console.log(val);
      this.pager.pageSize = val;
      this.fetcData(this.pager);
    },
    // 拉取后台数据（自带分页效果）
    fetcData(pager) {
      getList(pager).then(({ data }) => {
        console.log(data);
        this.tableData = data.data.data;
        this.pager = data.data.pager;
      });
    },
  },
};
</script>